package com.kanche.bigbaby;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.kanche.bigbaby.bean.ResponseBaseBean;
import com.kanche.bigbaby.bean.ResponseMcSigninBean;
import com.kanche.bigbaby.bean.SearchForm;
import com.kanche.bigbaby.collection.StringUtils;
import com.kanche.bigbaby.collection.domain.Vehicle;
import com.squareup.okhttp.OkHttpClient;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import retrofit.Callback;
import retrofit.RequestInterceptor;
import retrofit.RestAdapter;
import retrofit.client.OkClient;
import retrofit.converter.GsonConverter;
import retrofit.http.Body;
import retrofit.http.POST;

/**
 * Created by Chenshao_Young on 15/5/20.
 */
public interface BBService {

    @POST("/merchant/sign_in")
    void merchantSignin(@Body Map<String,String> params, Callback<ResponseMcSigninBean> callback);
    @POST("/inventory/create")
    void upLoadVehicle(@Body Vehicle vehicle, Callback<ResponseBaseBean> callback);
    @POST("/inventory/search")
    void searchVehicle(@Body SearchForm searchForm, Callback<ResponseBaseBean> callback);


    class Creator{
        static BBService bbService;

        public static BBService create(String endpoint, Context context, boolean requireFuckingParams){
            String apiEndpoint;
            if (!StringUtils.isHttpPath(endpoint)) {
                apiEndpoint = AppConfig.SERVER_END_POINT_BB;
            } else {
                apiEndpoint = endpoint;
            }

            Gson gson = new GsonBuilder().create();
            OkHttpClient httpClient = new OkHttpClient();
            httpClient.setReadTimeout(120, TimeUnit.SECONDS);
            httpClient.setConnectTimeout(30, TimeUnit.SECONDS);
            OkClient okClient = new OkClient(httpClient);

            RestAdapter.Builder builder = new RestAdapter.Builder()
                    .setEndpoint(apiEndpoint)
                    .setLogLevel(AppConfig.RETROFIT_LOG_LEVEL)
                    .setConverter(new GsonConverter(gson))
                    .setClient(okClient);

            if (requireFuckingParams) {
                String t = null;
//            if (context != null) {
//                BigBabyApplication bssApplication = (BigBabyApplication) context.getApplicationContext();
//                t = bssApplication.getBBSDBHelperInstance().findUserToken();
//            }


                final String token = t;
                RequestInterceptor requestInterceptor = new RequestInterceptor() {
                    @Override
                    public void intercept(RequestFacade request) {
//                        request.addHeader("User-Agent", "Kanche Merchant App - Android");
                        request.addHeader("Content-Type", "application/json");
                        if (StringUtils.hasText(token)) {
                            request.addHeader("X-AUTH-TOKEN", token);
                        }
                    }
                };
                builder.setRequestInterceptor(requestInterceptor);
            }
            bbService = builder.build().create(BBService.class);

            return bbService;
        }

        public static BBService create(Context context) {
            return create(null, context, true);
        }

    }
}
